package com.example.alibabavodossservice.vod.media;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.vod.model.v20170321.*;
import com.example.alibabavodossservice.CheckTools;
import org.springframework.beans.BeanUtils;

import java.util.List;

import static com.example.alibabavodossservice.vod.InitVODConstants.VOD_CLIENT;


/**
 * desc: 处理媒资指令集
 *
 * @author xugenyin
 */
public class Cmd {
    /**
     * 获取辅助媒资上传凭证
     * 如果上传媒资凭证失效（有效期3000秒），重新调用此接口获取上传地址和凭证
     * https://help.aliyun.com/zh/vod/developer-reference/api-vod-2017-03-21-createuploadattachedmedia
     * @param attachedMediaParameter 主要请求参数
     * @return CreateUploadAttachedMediaResponse 获取辅助媒资上传凭证请求响应
     * @throws ClientException 连接异常
     */
    public static CreateUploadAttachedMediaResponse createUploadAttachedMedia(AttachedMediaParameter attachedMediaParameter) throws ClientException {
//        AttachedMediaParameter.checkMustParam(attachedMediaParameter);
        CreateUploadAttachedMediaRequest uploadAttachedMediaRequest = new CreateUploadAttachedMediaRequest();
        uploadAttachedMediaRequest.setAcceptFormat(FormatType.JSON);
        //设置请求超时时间
        if (!CheckTools.isNullOrEmpty(attachedMediaParameter.getSysReadTimeout())) {
            uploadAttachedMediaRequest.setSysReadTimeout(1000);
        }
        if (!CheckTools.isNullOrEmpty(attachedMediaParameter.getSysConnectTimeout())) {
            uploadAttachedMediaRequest.setSysConnectTimeout(1000);
        }
        BeanUtils.copyProperties(uploadAttachedMediaRequest, attachedMediaParameter);
        return VOD_CLIENT.getAcsResponse(uploadAttachedMediaRequest);
    }

    /**
     * 注册媒资信息
     * 针对已接入点播服务的OSS bucket，已存储在OSS bucket上的音视频媒体文件，
     * 需要通过注册媒资接口生成点播需要的关联数据后，才能在点播服务中对这些媒体文件通过媒体ID发起转码、截图和AI处理等。
     * 在视频上传到VOD平台后，可以使用该API将视频与相关的元数据关联起来，以便更好地管理和检索视频资源。
     * 注册媒资信息API允许用户提供视频的相关信息，如标题、描述、标签、分类等，将这些元数据与视频文件进行绑定。
     *
     * @return RegisterMediaResponse 注册媒资信息响应数据
     * @throws Exception
     */
    public static RegisterMediaResponse registerMedia() throws Exception {
        RegisterMediaRequest request = new RegisterMediaRequest();
        JSONArray metaDataArray = new JSONArray();
        JSONObject metaData = new JSONObject();
        //标题
        metaData.put("Title", "我是标题");
        metaData.put("Tags", "我是标签");
        metaData.put("Description", "对视频的描述");
        //源文件地址，
        metaData.put("FileURL", "https://outin-d5f4905ead3111ec99b200163e1c955c.oss-cn-shanghai.aliyuncs.com/sv/462dd922-18a41711e8e/462dd922-18a41711e8e.mp4?Expires=1693375657&OSSAccessKeyId=LTAIxSaOfEzCnBOj&Signature=UovSgSt0rlSSLodjlKwfJkSe%2F4Q%3D");
        //TODO:文件名需要保证全局唯一。添加相同文件名称时关联唯一媒体ID。
        //指定要注册的媒资元数据
        metaDataArray.add((metaData));
        request.setRegisterMetadatas(metaDataArray.toJSONString("RegisterMetadatas"));
        return VOD_CLIENT.getAcsResponse(request);
    }

    /**
     * 创建媒资分类
     *
     * @param categoryParameter 分类参数
     * @return 分类实体
     * @throws ClientException 连接异常
     */
    public static AddCategoryResponse.Category createCate(CategoryParameter categoryParameter) throws ClientException {
        AddCategoryRequest addCategoryRequest = new AddCategoryRequest();
        BeanUtils.copyProperties(categoryParameter, addCategoryRequest);
        AddCategoryResponse addCategoryResponse = VOD_CLIENT.getAcsResponse(addCategoryRequest);
        return addCategoryResponse.getCategory();
    }

    /**
     * 搜索媒资信息
     *
     * @param searchMediaParameter 参数
     * @return SearchMediaResponse 搜索媒资信息响应数据
     * @throws Exception
     */
    public static SearchMediaResponse searchMedia(SearchMediaParameter searchMediaParameter) throws Exception {
        SearchMediaRequest searchMediaRequest = new SearchMediaRequest();
        BeanUtils.copyProperties(searchMediaParameter, searchMediaRequest);
        SearchMediaResponse acsResponse = VOD_CLIENT.getAcsResponse(searchMediaRequest);
        return VOD_CLIENT.getAcsResponse(searchMediaRequest);
    }

    /**
     * 媒资分组列表
     *
     * @param searchCategoryParameter 搜索参数
     * @return List<GetCategoriesResponse.Category> List<GetCategoriesResponse.Category>
     * @throws ClientException 异常
     */
    public static List<GetCategoriesResponse.Category> queryCateById(SearchCategoryParameter searchCategoryParameter) throws ClientException {
        GetCategoriesRequest getCategoriesRequest = new GetCategoriesRequest();
        BeanUtils.copyProperties(searchCategoryParameter, getCategoriesRequest);
        GetCategoriesResponse getCategoriesResponse = VOD_CLIENT.getAcsResponse(getCategoriesRequest);
        return getCategoriesResponse.getSubCategories();
    }

    /**
     * 获取辅助媒资信息
     *
     * @param MediaIds 媒资ID 多个ID之间使用半角逗号（,）分隔，最多支持20个。
     * @return
     * @throws ClientException
     */
    public static GetAttachedMediaInfoResponse getAttachedMediaInfo(String MediaIds) throws ClientException {
        GetAttachedMediaInfoRequest getAttachedMediaInfoRequest = new GetAttachedMediaInfoRequest();
        getAttachedMediaInfoRequest.setMediaIds(MediaIds);
        return VOD_CLIENT.getAcsResponse(getAttachedMediaInfoRequest);
    }

    /**
     * 修改媒资分类名称
     *
     * @param cateId   分类ID
     * @param cateName 分类名称
     * @return UpdateCategoryResponse 修改媒资分类名称响应数据
     * @throws ClientException 连接异常
     */
    public static UpdateCategoryResponse updateCategoryName(Long cateId, String cateName) throws ClientException {
        UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest();
        updateCategoryRequest.setCateId(cateId);
        updateCategoryRequest.setCateName(cateName);
        return VOD_CLIENT.getAcsResponse(updateCategoryRequest);
    }

    /**
     * 更新辅助媒资信息
     *
     * @param updateContent 更新内容 JSON格式字符串，本方法JSON自动转换
     * @return UpdateAttachedMediaInfosResponse 更新辅助媒资信息响应数据
     * @throws ClientException 连接异常
     */
    public static UpdateAttachedMediaInfosResponse updateAttachedMediaInfos(String updateContent) throws ClientException {
        UpdateAttachedMediaInfosRequest updateAttachedMediaInfosRequest = new UpdateAttachedMediaInfosRequest();
        updateAttachedMediaInfosRequest.setUpdateContent(updateContent);
        return VOD_CLIENT.getAcsResponse(updateAttachedMediaInfosRequest);
    }

    /**
     * 更新辅助媒资信息
     *
     * @param attachedMediaList 更新内容 媒资信息列表
     * @return UpdateAttachedMediaInfosResponse 更新辅助媒资信息响应数据
     * @throws ClientException 连接异常
     */
    public static UpdateAttachedMediaInfosResponse updateAttachedMediaInfos(List<GetAttachedMediaInfoResponse.AttachedMedia> attachedMediaList) throws ClientException {
        UpdateAttachedMediaInfosRequest updateAttachedMediaInfosRequest = new UpdateAttachedMediaInfosRequest();
        updateAttachedMediaInfosRequest.setUpdateContent(JSONArray.toJSONString(attachedMediaList));
        return VOD_CLIENT.getAcsResponse(updateAttachedMediaInfosRequest);
    }

    /**
     * 删除媒资分类
     *
     * @param cateId 分类ID
     * @throws ClientException 连接异常
     */
    public static UpdateCategoryResponse deleteCategory(Long cateId) throws ClientException {
        UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest();
        updateCategoryRequest.setCateId(cateId);
        return VOD_CLIENT.getAcsResponse(updateCategoryRequest);
    }


}
